package org.study.neo4j.boot.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.study.neo4j.boot.dto.CourseDTO;
import org.study.neo4j.boot.model.Course;
import org.study.neo4j.boot.service.CourseService;
import org.study.neo4j.boot.util.ConvertToDtoUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月09日
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RestController
@RequestMapping("/api/courses")
public class CourseController {
    private final CourseService courseService;

    @GetMapping
    public List<CourseDTO> getAllCourses() {
        return courseService.getAllCourses().stream()
                .map(ConvertToDtoUtils::convertToCourseDTO)
                .collect(Collectors.toList());
    }

    @GetMapping("/{code}")
    public CourseDTO getCourseByCode(@PathVariable("code") String code) {
        return ConvertToDtoUtils.convertToCourseDTO(courseService.getCourseByCode(code));
    }

    @PostMapping
    public CourseDTO createCourse(@Validated @RequestBody Course course) {
        return ConvertToDtoUtils.convertToCourseDTO(courseService.saveCourse(course));
    }

    @GetMapping("/by-teacher/{teacherName}")
    public List<CourseDTO> getCoursesByTeacher(@PathVariable("teacherName") String teacherName) {
        return courseService.getCoursesByTeacherName(teacherName).stream()
                .map(ConvertToDtoUtils::convertToCourseDTO)
                .collect(Collectors.toList());
    }

    @GetMapping("/by-student/{studentName}")
    public List<CourseDTO> getCoursesByStudent(@PathVariable("studentName") String studentName) {
        return courseService.getCoursesByStudentName(studentName).stream()
                .map(ConvertToDtoUtils::convertToCourseDTO)
                .collect(Collectors.toList());
    }

    @DeleteMapping("/{code}")
    public void deleteCourse(@PathVariable("code") String code) {
        courseService.deleteCourse(code);
    }
}
